package com.mtt.erp.freight.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.mtt.erp.common.annotation.Log;
import com.mtt.erp.common.api.R;
import com.mtt.erp.common.api.ResultCode;
import com.mtt.erp.common.controller.BaseController;
import com.mtt.erp.common.domain.QueryRequest;
import com.mtt.erp.freight.dto.McbCreateDto;
import com.mtt.erp.freight.dto.McbDto;
import com.mtt.erp.freight.dto.McbUpdateDto;
import com.mtt.erp.freight.service.IMcbFileService;
import com.mtt.erp.freight.vo.McbFileDetailVo;
import com.mtt.erp.freight.vo.McbFileVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;

import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * 运费模板
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/freight/mcb")
@Api(value = "运费模板", tags = "M005-物流-运费模板接口")
public class McbFileController {
    @Autowired
    private IMcbFileService iMcbFileService;

    @Log("模板信息")
    @GetMapping
    //@RequiresPermissions("freight:view")
    @ApiOperation(value = "运费模板列表", notes = "根据条件查询运费模板列表,mcbDto", position = 1)
    public R<IPage<McbFileDetailVo>> mcbList(QueryRequest request, McbDto mcbDto){
        IPage<McbFileDetailVo> mcaDetailPage = iMcbFileService.findMcb(request, mcbDto);
        // 判断结果数
        if (mcaDetailPage.getTotal()>0){
            return R.data(mcaDetailPage);
        } else {
            return R.fail(ResultCode.NO_CONTENT);
        }
    }

    @Log("添加模板")
    @PostMapping
    @RequiresPermissions("freight:add")
    @ApiOperation(value = "新增运费模板", notes = "将运费模板保存到数据库,mcbCreateDto", position = 2)
    public R addMcb(@Valid McbCreateDto mcbCreateDto) {
        try {
            this.iMcbFileService.createMcb(mcbCreateDto);
            return R.success(ResultCode.SUCCESS);
        } catch (Exception e) {
            return R.fail(ResultCode.FAILURE);
        }
    }

    @Log("修改模板")
    @PutMapping
    @RequiresPermissions("freight:update")
    @ApiOperation(value = "修改运费模板", notes = "根据运费模板id修改运费模板信息,mcbUpdateDto", position = 3)
    public R updateMcb(@Valid McbUpdateDto mcbUpdateDto){
        try {
            this.iMcbFileService.updateMcb(mcbUpdateDto);
            return R.success(ResultCode.SUCCESS);
        } catch (Exception e) {
            return R.fail(ResultCode.FAILURE);
        }
    }

    @Log("删除模板")
    @DeleteMapping("/{mcbIds}")
    @RequiresPermissions("freight:delete")
    @ApiOperation(value = "删除运费模板", notes = "批量删除运费模板,传入要删除的运费模板id串,以逗号分隔", position = 4)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "id串,以逗号分割", name = "mcbIds",required=true,paramType = "path")
    })
    public R deleteMcb(@NotBlank(message = "{required}") @PathVariable String mcbIds) {
        try {
            String[] ids = mcbIds.split(StringPool.COMMA);
            this.iMcbFileService.deleteMcb(ids);
            return R.success(ResultCode.SUCCESS);
        } catch (Exception e) {
            return R.fail(ResultCode.FAILURE);
        }
    }

    /**
     * 运费模板对象
     *
     * @author guomengzhou
     */
    @Log("通过id查询运费模板信息")
    @GetMapping("/{id}")
    @ApiOperation(value = "运费模板id", notes = "根据模板id查询运费信息", position = 5)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "运费模板id", required=true,paramType = "path")
    })
    public R<McbFileDetailVo> getCategoryById(@NotBlank(message = "{required}") @PathVariable String id) {
        McbFileDetailVo mcbFileDetailVo = this.iMcbFileService.findCategoryById(id);
        if (mcbFileDetailVo != null) {
            return R.data(mcbFileDetailVo);
        }else{
            return R.fail(ResultCode.NO_CONTENT);
        }
    }
}
